<?php

/**
 * @see SmartL_Zend_Controller_Action
 */
require_once 'SmartL/Zend/Controller/Action.php';

require_once 'Db/Record/User.php';
require_once 'Doctrine/Query.php';

class FrameController extends SmartL_Zend_Controller_Action 
{		
	public function loginFormAction()
	{
		if ( !Zend_Auth::getInstance()->hasIdentity() ) {
			$this->view->usernames = array('steve','vojta');
			$this->_loginForm();
		}
		else {			
			$loginResult = iDo_Login::getLoginResult();
			if ( !empty($loginResult) ) {
				//$this->_helper->redirector->setGotoRoute(array('action'=>'index'), 'default', false);
			}
		}
	}
	
	public function usersListAction()
	{
		$this->_users();
	}	
	
	private function _loginForm()
	{
		$result = iDo_Login::getLoginResult();
		$form = iDo_Login::getLoginForm();
		
		if ( empty($result) and empty($form) ) {
			$form = SmartL_Zend_Form::getForm($this->config->getAction('loginForm')->getForm('login'), $this->translations['default']);	
		}
		else {			
			if ( !empty($result)) {	
				if ( !$result->isValid() ) {					
					$form->password->addError($this->translations['default']->translate('wrongLogin'));
				}
			}
			$this->translations['default']->addTranslation(SmartL_Zend_Form::getErrorsTranslateFilename());
			$form->setTranslator($this->translations['default']);
		}
				
		$this->view->loginForm = $form;
	}
	
	private function _users()
	{		
		$query = new Doctrine_Query();
		$query->from('Db_Record_User u')
			->orderBy('username');		

		$this->view->users = $users = $query->fetchArray();
	}
}